<?php
//供应商
namespace Common\Model;

class UserKuerpWarehouseProductModel extends UserKuerpWarehouseModel {

	//追加属性
    public $append = [
        //'edit_time_text',
        'wh_name',          //仓库名称
        'product_name',     //商品名
        'product_info',     //商品信息
	];

    /**
     * 仓库信息
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getWhNameAttr($value, $data){
        $name = '';
		$model = M('UserKuerpWarehouse');
		$wdata['id'] = $data['wh_id'];
		$name = $model->where($wdata)->getField('name') ? : '';
		return $name;
    }

    /**
     +----------------------------------------------------------
     * 列表信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $map 查询数据
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
    */
	public function getList2($map = array(), $listRows=20 ,$_order_sort =''){
        //生成查询条件
        //列表过滤器，生成查询Map对象
        $map = $map ? $map : $this->_search2();
        if (method_exists($this, '_filter')) {
            $this->_filter($map);
        }
		$data = $this->_list2($this, $map, $listRows ,$_order_sort);
		return $data;
	}

    //搜索条件
    public function _after_search(){
        $input = I('request.');
        $data = array();
        /*
        if ($input['product_name']) {
            $model = M('Product');
            $wdata['name'] = array('in',$input['product_name']);
            $list = $model->where($wdata)->field('id')->select();
            if($list){
                foreach($list as $val){
                    $product_ids[] = $val['id'];
                }
                $data['product_id'] = array('in',$product_ids);
            }else{
                $data['product_id'] = 0;
            }
        }
        */
        if ($input['product_id']) {
            $data['product_id'] = array('in',$input['product_id']);
        }
        return $data;
    }


   /**
      +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
      +----------------------------------------------------------
     * @access protected
      +----------------------------------------------------------
     * @param Model $model 数据对象
     * @param HashMap $map 过滤条件
     * @param string $sortBy 排序
     * @param boolean $asc 是否正序
      +----------------------------------------------------------
     * @return void
      +----------------------------------------------------------
     */
    public function _list2($model, $map, $listRows='' ,$sortBy ='' , $field='*') {
        //排序字段 默认为主键名
        if (isset($_REQUEST ['_order'])) {
            $order = $_REQUEST ['_order'];
        } else {
            $order = !empty($sortBy) ? $sortBy : $model->getPk();
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if (isset($_REQUEST ['_sort'])) {
            $sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        //取得满足条件的记录数
        $model = D('UserWarehouseProduct');
        $count = $model->alias('wp')->join('__PRODUCT__ p on wp.product_id=p.id','LEFT')->where($map)->count();
        //echo M('')->getlastsql();exit;
        if (!empty($_REQUEST ['listRows'])) {
            $listRows = $_REQUEST ['listRows'];
        } else {
            $page_size = C('page_size');
            $listRows = $page_size ? $page_size : 20;
        }
        $page_count = ceil($count / $listRows);
        if ($count > 0) {
            //创建分页对象
            $p = new \Think\AdminPage($count, $listRows);
            //分页查询数据
            
            $voList = $model->alias('wp')->join('__PRODUCT__ p on wp.product_id=p.id','LEFT')->field('p.*,wp.stock')->where($map)->order("`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select();
            //echo M('')->getlastsql();exit;
            //分页跳转的时候保证查询条件
            foreach ($map as $key => $val) {
                if (!is_array($val)) {
                    $p->parameter .= "$key=" . urlencode($val) . "&";
                }
            }
            //type
            $poi_model = M('PurchaseOrderItems');
            $soi_model = M('SaleOrderItems');
            foreach($voList as &$val){
                if($map['wp.wh_id']){
                    if(I('request.type')=='purchase'){
                        //采购价格
                        $poi_wdata['wh_id'] = $map['wp.wh_id'];
                        $price = $poi_model->where($poi_wdata)->order('id desc')->getField('price');
                        $val['price'] = floatval($price);
                    }else if(I('request.type')=='sale'){
                        //销售价格
                        $soi_wdata['wh_id'] = $map['wp.wh_id'];
                        $price = $soi_model->where($soi_wdata)->order('id desc')->getField('price');
                        $val['price'] = floatval($price);
                    }else{
                        $val['price'] = '--';
                    }
                    
                }else{
                    $val['price'] = '--';
                }
                //dump($val['price']);exit;
                $val['stock'] = floatval($val['stock']);
            }
            //分页显示
            $page = $p->show();

            //列表排序显示
            $sortImg = $sort; //排序图标
            $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
            $sort = $sort == 'desc' ? 1 : 0; //排序方式
            //模板赋值显示
        }
        $data['list'] = $voList;
        $data['sort'] = $sort;
        $data['order'] = $order;
        $data['sortType'] = $sortType;
        $data['page'] = $page;
        $data['map'] = $map;
		$data['page_count'] = $p->totalPages;//总页数
		$data['count'] = $p->totalRows;//总条数
        $data['listRows'] = $p->listRows;//总条数
		$data['now_page'] = $p->nowPage;
        return $data;
    }

    //搜索条件
    public function _search2(){
        $input = I('request.');

        if ($input['id']) {
            $data['p.id'] = array('eq', $input['id']);
        }

        if ($input['name']) {
            $data['p.name'] = array('like', '%'.$input['name'].'%');
        }

        if ($input['wh_id']) {
            $data['wp.wh_id'] = $input['wh_id'];
        }
        if ($input['cat_id']) {
            $data['p.cat_id'] = $input['cat_id'];
        }
        return $data;
    }
   
}

?>